package actions;

/**
 * 对于域对象的新增、修改等操作以及其他的涉及到表单提交（录入界面）的业务操作， 强烈建议采用Struts
 * 动态校验FormBean来实现基于Validator的Form字段校验、重复提交等必要功能。
 */
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;

import service.UserAccountService;
import domain.UserAccount;

/**
 * 对于域对象的新增、修改等操作以及其他的涉及到表单提交（录入界面）的业务操作， 强烈建议采用Struts
 * 动态校验FormBean来实现基于Validator的Form字段校验、重复提交等必要功能。
 */
/**
 * 编辑用户账号的Action对象.
 * @author zhuweiguo
 */
public class ChangePasswordAction extends Action {
    /**userAccountService用来完成用户帐号操作的业务逻辑 .**/
    private UserAccountService userAccountService = null;

    /**
     * 更改用户帐号密码.
     * @param mapping aciton mapping
     * @param form  struts form
     * @param request http request
     * @param response http response
     * @return action forward
     * @throws Exception any exception
     */
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        // 验证是否是有效的Token,防止重复提交
        if (isTokenValid(request, true)) {
            DynaValidatorForm newPasswordForm = (DynaValidatorForm) form;
            UserAccount curUser = (UserAccount) request.getSession()
                    .getAttribute("HUAXIA_CURRENT_USER");
            ActionMessages errMsgs = new ActionMessages();
            if (!curUser.getPassword().equals(
                    newPasswordForm.getString("oldPassword"))) {
                // 原密码不符
                errMsgs.add("oldPassword", new ActionMessage(
                        "errors.wrongOldPassword"));

            }
            String newPassword = newPasswordForm.getString("newPassword1");
            String newPassord2 = newPasswordForm.getString("newPassword2");
            if (!newPassword.equals(newPassord2)) {// 密码不匹配
                errMsgs.add("oldPassword2", new ActionMessage(
                        "errors.notMatchedPasswords"));
            }
            if (errMsgs.size() > 0) {// 有错误信息
                this.saveErrors(request, errMsgs);
                // 重新设置token
                this.saveToken(request);
                return mapping.getInputForward();
            }

            // 调用UserAccountService更改用户账号
            userAccountService.updatePassword(curUser.getUserID(), newPassword);
            // 提示操作完成，从资源文件中获取常量字符串
            String msg = getResources(request).getMessage(
                    "info.SucsessChangedPassword");
            request.setAttribute("INFO_MSG", msg);
            return mapping.findForward("info.return");
        } else {
            // 重复提交,转到重复提交失败界面
            return mapping.findForward("error.duplicateSubmit");
        }
    }

    /**
     * @param pUserAccountService The userAccountService to set.
     */
    public void setUserAccountService(UserAccountService pUserAccountService) {
        this.userAccountService = pUserAccountService;
    }

}
